﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace SelElectCalcModernUI.Lib
{
    public class YuanToLiConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value * 1000;
            else
                return 0;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value / 1000;
            else
                return 0;
        }
    }
    public class LiToYuanConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value / 1000;
            else
                return 0;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value * 1000;
            else
                return 0;
        }
    }
    public class PerConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value * 100;
            else
                return 0;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value / 100;
            else
                return 0;
        }
    }
    public class PerRevConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value / 100;
            else
                return 0;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((value is double && double.IsNaN((double)value)) ||
                (value is float && float.IsNaN((float)value)) ||
                (value is int && ((int)value) < 0) ||
                (value is int && ((int)value) < 0))
                return null;
            if (value is double || value is float || value is int || value is long)
                return (double)value * 100;
            else
                return 0;
        }
    }
    public class TenThousandConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double || value is float || value is int || value is long)
                return (double)value * 10000;
            else
                return 0;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double || value is float || value is int || value is long)
                return (double)value / 10000;
            else
                return 0;
        }
    }
    public class TenThousandRevConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double || value is float || value is int || value is long)
                return (double)value / 10000;
            else
                return 0;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double || value is float || value is int || value is long)
                return (double)value * 10000;
            else
                return 0;
        }
    }
    public class DoubleNanDisableConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is double && !double.IsNaN((double)value))
                return (double)value;
            else
                return string.Empty;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double res = double.NaN;
            if (string.IsNullOrWhiteSpace(value.ToString()))
                return double.NaN;
            else if (double.TryParse(value.ToString(), out res))
                return res;
            else
                return res;
        }
    }
    public class IntNanDisableConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int && (int)value > -1)
                return (int)value;
            else
                return string.Empty;
            //throw new NotImplementedException();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int res = -1;
            if (!string.IsNullOrWhiteSpace(value.ToString()) && int.TryParse(value.ToString(), out res))
                return res;
            else
                return res;
        }
    }
}
